﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using consultasSql.consultasVarias;
using consultasSql.generic.Insumo;

/// <summary>
/// Descripción breve de CConsultaInsumo
/// </summary>

namespace consultasSql.generic.Accesorio
{
    public class CConsultaInsumo : CDarDataSet
	{
        public DataSet obtenerInsumoPorTipoInsumoId(int tipoInsumoId)
        {
            string consulta = "SELECT * " +
            " FROM Insumos " +
            " WHERE tipoInsumoId = @tipoInsumoId ORDER BY descripcion";

            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.Text;
            command.CommandText = consulta;
            command.Parameters.Clear();

            command.Parameters.Add(new SqlParameter("@tipoInsumoId", SqlDbType.Int));
            command.Parameters["@tipoInsumoId"].Value = tipoInsumoId;

            return darDataSet(command);
        }

        # region Lista de Muchos a Muchos
        public bool deleteAllMuchosAMuchos(int bolsaId, SqlConnection conn, SqlTransaction tr, string table)
        {
            bool bandera = false;

            string consulta = "DELETE FROM " + table + 
                " WHERE objetoId = @objetoId";

            using (SqlCommand command = conn.CreateCommand())
            {
                if (conn.State == ConnectionState.Closed)
                { conn.Open(); }

                command.CommandType = CommandType.Text;
                command.CommandText = consulta;
                command.Transaction = tr;
                command.Parameters.Clear();

                command.Parameters.Add(new SqlParameter("@objetoId", bolsaId));

                try
                {
                    command.ExecuteNonQuery();
                    bandera = true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return bandera;
        }

        public bool insertMuchosAMuchos(CInsumo insumo, int objetoId, SqlConnection conn, SqlTransaction tr, string table)
        {
            bool bandera = false;

            string consulta = "INSERT INTO " + table + "(objetoId, insumoId)" +
                " VALUES (@objetoId, @insumoId)";

            using (SqlCommand command = conn.CreateCommand())
            {
                if (conn.State == ConnectionState.Closed)
                { conn.Open(); }

                command.CommandType = CommandType.Text;
                command.CommandText = consulta;
                command.Transaction = tr;
                command.Parameters.Clear();

                command.Parameters.Add(new SqlParameter("@objetoId", objetoId));
                command.Parameters.Add(new SqlParameter("@insumoId", insumo.id));
                
                try
                {
                    command.ExecuteNonQuery();
                    bandera = true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return bandera;
        }
        # endregion
    }
}